package com.unit.test.case2.config;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextLoader;


public abstract class BaseConfigContextLoader implements ContextLoader{

    private Logger log = LoggerFactory.getLogger(getClass());

    protected String[] springXmlList = null;

    @Override
    public String[] processLocations(Class<?> clazz, String... locations){
        if (locations != null && locations.length >= 1){
            String[] xmlList = new String[springXmlList.length + locations.length];
            System.arraycopy(springXmlList, 0, xmlList, 0, springXmlList.length);
            System.arraycopy(locations, 0 , xmlList, springXmlList.length, locations.length);
            springXmlList = xmlList;
        }

        List<String> xmlList = new ArrayList<>(springXmlList.length);
        for ( String  xml : springXmlList){
            if (xmlList.contains(xml)){
                log.error("{msg:'存在重复的spring xml文件',filePath:[{}]}",xml);
            }else {
                xmlList.add(xml);
            }
        }
        springXmlList = xmlList.toArray(springXmlList);
        return springXmlList;
    }

    @Override
    public ApplicationContext loadContext(String... locations) {

        return  new ClassPathXmlApplicationContext(locations);
    }

}
